package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    /**
     * 条件分页查询菜品
     */
    @GetMapping("/page")
    public Result pageSelect(DishPageQueryDTO query) {
        PageResult pageResult = dishService.selectPage(query);
        return Result.success(pageResult);
    }


    /**
     * 回显
     * 根据id查询菜品
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id) {
        DishVO dishVO = dishService.getById(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品
     */
    @PutMapping
    public Result updateById(@RequestBody DishDTO dishDTO) {
        dishService.updateById(dishDTO);
        return Result.success();
    }

    /**
     * 批量删除菜品
     */
    @DeleteMapping
    public Result deleteByIds(@RequestParam List<Integer> ids) {
        dishService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 新增菜品
     */
    @PostMapping
    public Result insertDish(@RequestBody DishDTO dishDTO) {
        dishService.insertDish(dishDTO);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     */
    @GetMapping("/list")
    public Result getListById(Long categoryId) {
        List<Dish> dishList = dishService.getListById(categoryId);
        return Result.success(dishList);
    }

    /**
     * 菜品起售、停售
     * 改变菜品的状态
     */
    @PostMapping("/status/{status}")
    public Result updateStatusById(@PathVariable Integer status,Long id){
        dishService.updateStatusById(id);
        return Result.success();
    }
}
